package com.example.itensyuan0609;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.itensyuan0609.R;
import com.example.itensyuan0609.bin.MyBean;
import com.example.itensyuan0609.fragment.OneFragment;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.util.List;

public class MyNewsAdapter extends BaseAdapter {
    List<MyBean.ResultBean.DataBean> dataList;
    Context context;

    public MyNewsAdapter(List<MyBean.ResultBean.DataBean> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    public MyNewsAdapter(List<MyBean.ResultBean.DataBean> myBeans, OneFragment inflater) {
    }


    /*
     * listview的item的数量
     * */
    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        int type = getItemViewType(i);
        if (view == null) {
            switch (type) {
                case 0:
                    viewHolder1 = new ViewHolder1();
                    view = LayoutInflater.from(context).inflate(R.layout.iten_news_one, null);
                    viewHolder1.tvTitleOne = view.findViewById(R.id.tv_title_one);
                    viewHolder1.tvDataOne = view.findViewById(R.id.tv_date_one);
                    viewHolder1.tvAuthorOne = view.findViewById(R.id.tv_author_one);
                    viewHolder1.imageViewone = view.findViewById(R.id.iv_one);
                    view.setTag(viewHolder1);
                    break;
                case 1:
                    viewHolder2 = new ViewHolder2();
                    view = LayoutInflater.from(context).inflate(R.layout.iten_news_tow, null);
                    viewHolder2.tvTitleTow = view.findViewById(R.id.tv_title_tow);
                    viewHolder2.tvDateTow = view.findViewById(R.id.tv_date_tow);
                    viewHolder2.tvAuthorTow = view.findViewById(R.id.tv_aurhor_tow);
                    viewHolder2.imageViewtow1 = view.findViewById(R.id.iv_tow_1);
                    viewHolder2.imageViewtow2 = view.findViewById(R.id.iv_tow_2);
                    viewHolder2.imageViewtow3 = view.findViewById(R.id.iv_tow_3);
                    view.setTag(viewHolder2);
                    break;
            }
        } else {
            switch (type) {
                case 0:
                    viewHolder1 = (ViewHolder1) view.getTag();
                    break;
                case 1:
                    viewHolder2 = (ViewHolder2) view.getTag();
                    break;
            }
        }
        MyBean.ResultBean.DataBean dataBean = dataList.get(i);
        Picasso picasso = Picasso.with(context);
        switch (type) {
            case 0:
                RequestCreator creator = picasso.load(dataBean.getThumbnail_pic_s());
                creator.into(viewHolder1.imageViewone);
                viewHolder1.tvTitleOne.setText(dataBean.getTitle());
                viewHolder1.tvAuthorOne.setText(dataBean.getAuthor_name());
                viewHolder1.tvDataOne.setText(dataBean.getDate());
                break;
            case 1:
                RequestCreator creator1 = picasso.load(dataBean.getThumbnail_pic_s());
                RequestCreator creator2 = picasso.load(dataBean.getThumbnail_pic_s02());
                RequestCreator creator3 = picasso.load(dataBean.getThumbnail_pic_s03());
                creator1.into(viewHolder2.imageViewtow1);
                creator2.into(viewHolder2.imageViewtow2);
                creator3.into(viewHolder2.imageViewtow3);
                viewHolder2.tvTitleTow.setText(dataBean.getTitle());
                viewHolder2.tvAuthorTow.setText(dataBean.getAuthor_name());
                viewHolder2.tvDateTow.setText(dataBean.getDate());
                break;
        }
        return view;
    }

    /*
     * listview要加载几个item
     * */
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    /*
     *
     * 获取当前加载的item的类型
     * */
    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getType();
    }

    class ViewHolder1 {
        ImageView imageViewone;
        TextView tvTitleOne, tvDataOne, tvAuthorOne;
    }

    class ViewHolder2 {
        ImageView imageViewtow1, imageViewtow2, imageViewtow3;
        TextView tvTitleTow, tvDateTow, tvAuthorTow;
    }
}
